package com.ruoyi.develop.lnGongyi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.develop.lnGongyigongxu.domain.LnGongyigongxu;
import com.ruoyi.develop.lnGongyigongxu.service.ILnGongyigongxuService;
import com.ruoyi.develop.lnGongyigoods.service.ILnGongyigoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lnGongyi.domain.LnGongyi;
import com.ruoyi.develop.lnGongyi.service.ILnGongyiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺Controller
 * @author qh
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/lnGongyi/lnGongyi")
public class LnGongyiController extends BaseController {
    @Autowired
    private ILnGongyiService lnGongyiService;
    @Autowired
    private ILnGongyigongxuService lnGongyigongxuService;

    @Autowired
    private ILnGongyigoodsService lnGongyigoodsService;


    /**
     * 查询工艺列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:query')")
    @GetMapping("/list")
    public TableDataInfo list(LnGongyi entity) {
        startPage();
        List<LnGongyi> list = lnGongyiService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出工艺列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:export')")
    @Log(title = "工艺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LnGongyi entity) {
        List<LnGongyi> list = lnGongyiService.selectList(entity);
        ExcelUtil<LnGongyi> util = new ExcelUtil<LnGongyi>(LnGongyi. class);
        util.exportExcel(response, list, "工艺数据");
    }

    /**
     * 获取工艺详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lnGongyiService.selectById(id));
    }

    /**
     * 新增工艺
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:add')")
    @Log(title = "工艺", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LnGongyi entity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(new Date());
        entity.setCode(time);
        return toAjax(lnGongyiService.insert(entity));
    }

    /**
     * 修改工艺
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:edit')")
    @Log(title = "工艺", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LnGongyi entity) {
        return toAjax(lnGongyiService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:edit')")
    @Log(title = "复制工艺", businessType = BusinessType.UPDATE)
    @PutMapping("/editTwo")
    public AjaxResult editTwo(@RequestBody LnGongyi entity) {
        return toAjax(lnGongyiService.updateTwo(entity));
    }

    /**
     * 删除工艺
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyi:remove')")
    @Log(title = "工艺", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        for(String id:ids){
            if(StringUtils.isNotBlank(id)) {
                lnGongyigongxuService.deleteByGongyiId(id);
                lnGongyigoodsService.deleteByGongyiId(id);
            }
            //List<LnGongyigongxu> lnGongyigongxus=lnGongyigongxuService.selectByGongyi(id);
        }
        return toAjax(lnGongyiService.deleteByIds(ids));
    }
}
